|
1
|
|
|
/* global hexo, __dirname */ |
|
2
|
|
|
|
|
3
|
|
|
'use strict'; |
|
4
|
|
|
|
|
5
|
|
|
const fs = require('hexo-fs'); |
|
6
|
|
|
const path = require('path'); |
|
7
|
|
|
const CryptoJS = require('crypto-js'); |
|
8
|
|
|
|
|
9
|
|
|
hexo.extend.filter.register('after_post_render', function encrypt (data) { |
|
10
|
|
|
|
|
11
|
|
|
// Close the encrypt function |
|
12
|
|
|
if (!('encrypt' in hexo.config && hexo.config.encrypt && 'enable' in hexo.config.encrypt && hexo.config.encrypt.enable)) { |
|
13
|
|
|
|
|
14
|
|
|
return data; |
|
15
|
|
|
|
|
16
|
|
|
} |
|
17
|
|
|
if (!('default_template' in hexo.config.encrypt && hexo.config.encrypt.default_template)) { // No such template |
|
18
|
|
|
|
|
19
|
|
|
hexo.config.encrypt.default_template = fs.readFileSync(path.resolve(__dirname, './template.html')); |
|
20
|
|
|
|
|
21
|
|
|
} |
|
22
|
|
|
if (!('default_abstract' in hexo.config.encrypt && hexo.config.encrypt.default_abstract)) { // No read more info |
|
23
|
|
|
|
|
24
|
|
|
hexo.config.encrypt.default_abstract = 'The article has been encrypted, please enter your password to view.<br>'; |
|
25
|
|
|
|
|
26
|
|
|
} |
|
27
|
|
|
if (!('default_message' in hexo.config.encrypt && hexo.config.encrypt.default_message)) { // No message |
|
28
|
|
|
|
|
29
|
|
|
hexo.config.encrypt.default_message = 'Please enter the password to read the blog.'; |
|
30
|
|
|
|
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
if ('password' in data && data.password) { |
|
34
|
|
|
|
|
35
|
|
|
// Use the blog's config first |
|
36
|
|
|
console.log(`encrypt the blog :${ data.title.trim() }`); |
|
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
// Store the origin data |
|
39
|
|
|
data.origin = data.content; |
|
40
|
|
|
data.encrypt = true; |
|
41
|
|
|
|
|
42
|
|
|
if (!('abstract' in data && data.abstract)) { |
|
43
|
|
|
|
|
44
|
|
|
data.abstract = hexo.config.encrypt.default_abstract; |
|
45
|
|
|
|
|
46
|
|
|
} |
|
47
|
|
|
if (!('template' in data && data.template)) { |
|
48
|
|
|
|
|
49
|
|
|
data.template = hexo.config.encrypt.default_template; |
|
50
|
|
|
|
|
51
|
|
|
} |
|
52
|
|
|
if (!('message' in data && data.message)) { |
|
53
|
|
|
|
|
54
|
|
|
data.message = hexo.config.encrypt.default_message; |
|
55
|
|
|
|
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
data.content = escape(data.content); |
|
59
|
|
|
data.content = CryptoJS.enc.Utf8.parse(data.content); |
|
60
|
|
|
data.content = CryptoJS.enc.Base64.stringify(data.content); |
|
61
|
|
|
data.content = CryptoJS.AES.encrypt(data.content, String(data.password)).toString(); |
|
62
|
|
|
// Console.log(data.content); |
|
63
|
|
|
data.template = data.template.replace('{{content}}', data.content); |
|
64
|
|
|
data.template = data.template.replace('{{message}}', data.message); |
|
65
|
|
|
data.template = data.template.replace('{{message}}', data.message); |
|
66
|
|
|
|
|
67
|
|
|
data.content = data.template; |
|
68
|
|
|
data.content += `<script src="${hexo.config.root}lib/crypto-js.js"></script> |
|
69
|
|
|
<script src="${hexo.config.root}lib/blog-encrypt.js"></script>' |
|
70
|
|
|
<link href="${hexo.config.root}css/blog-encrypt.css" rel="stylesheet" type="text/css">`; |
|
71
|
|
|
|
|
72
|
|
|
data.more = data.abstract; |
|
73
|
|
|
data.excerpt = data.more; |
|
74
|
|
|
|
|
75
|
|
|
} |
|
76
|
|
|
return data; |
|
77
|
|
|
|
|
78
|
|
|
}); |
|
79
|
|
|
|
|
80
|
|
|
hexo.extend.generator.register('blog-encrypt', () => [ |
|
81
|
|
|
{ |
|
82
|
|
|
'data': () => fs.createReadStream(path.resolve(path.dirname(require.resolve('crypto-js')), 'crypto-js.js')), |
|
83
|
|
|
'path': 'lib/crypto-js.js', |
|
84
|
|
|
}, { |
|
85
|
|
|
'data': () => fs.createReadStream(path.resolve(__dirname, 'lib/blog-encrypt.js')), |
|
86
|
|
|
'path': 'lib/blog-encrypt.js', |
|
87
|
|
|
}, { |
|
88
|
|
|
'data': () => fs.createReadStream(path.resolve(__dirname, 'lib/blog-encrypt.css')), |
|
89
|
|
|
'path': 'css/blog-encrypt.css', |
|
90
|
|
|
}, |
|
91
|
|
|
]); |
|
92
|
|
|
|